'''
Author: your name
Date: 2021-03-31 10:44:36
LastEditTime: 2021-03-31 15:50:12
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/test/getdata.py
'''

import json
import ssl
from urllib.request import Request, urlopen
import xlwt

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('demo1',cell_overwrite_ok=True)
# 合并单元格, 前四个参数为需要合并的单元格的序号, Test为单元格内容, style为单元格样式
worksheet.write_merge(0, 0, 0, 2, label='标题')
    # 设置列宽, 3为列的数目, 12为列的宽度, 256为固定值
for i in range(3):
    worksheet.col(i).width = 256 * 12
# 设置单元格行高, 25为行高, 20为固定值
worksheet.row(1).height_mismatch = True
worksheet.row(1).height = 20 * 25

#使用ssl创建未经验证的上下文，在urlopen中传入上下文参数
context = ssl._create_unverified_context()

#包装头部
firefox_headers = {
    'Accept':'application/json, text/plain, */*',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Authorization':'Bearer 228a742d-e243-4d6b-bd90-5dd15b1e0706',
    'Connection':'keep-alive',
    'Cookie':'Qs_lvt_283052=1617160502%2C1617161768%2C1617162151%2C1617176770%2C1617176785; Qs_pv_283052=1185793034107121400%2C671573483512934100%2C4398440428423820300%2C1308095977310219000%2C75114034464704600; wzws_cid=769cecdfe62c498bfa7034e1f62825e6c2ea7972dcbc78dd4c20659a2a0d92dc7f02c5fae406d9d52fab791e19eae7572dde69e3af8bf5f3c30c5978c9c4247b0d02b6b4bed8834c63db2714ab252f16',
    # 'Referer':'https://codescan.qianxin.com/project/12032/overview?pageIndex=13&pageSize=10',
    'Sec-Fetch-Dest':'empty',
    'Sec-Fetch-Mode':'cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63'
    }

i =1
#zb网站获取数据Api
for pages in  range(80,101):
    url = "https://codescan.qianxin.com/api/result-process/audit/bugs/12032?pageIndex="+str(pages)+"&pageSize=50"
    #构建请求
    request = Request( url, headers=firefox_headers )
    html = urlopen( request,context=context )
    #获取数据
    data = html.read()
    #转换成字符串
    strs = str(data)
    #截取字符串
    datas = json.dumps(strs)
    obj = json.loads(data)
    jx = obj['data']
    # print(obj['data'])
    for s in jx:
        print(str(s['bugLevel']))
        if(str(s['bugLevel'])!='4'):
            i+=1
            print('i:'+str(i))
            # print('缺陷类型:'+s['bugType'])
            if(str(s['bugLevel'])=='2'):
                worksheet.write(i, 1, '中危')
                print('bug级别:'+'中危')
            elif(str(s['bugLevel'])=='1'):
                print('bug级别:'+'低微')
                worksheet.write(i, 1, '低微')
            # print('文件位置:'+s['filePath'])
            worksheet.write(i, 0, str(s['bugType']))
            # worksheet.write(i, 1, str(s['bugLevel']))
            worksheet.write(i, 2, str(s['filePath']))
    # 保存excel文件
    workbook.save('excelTest.xls')